package com.wm.offer.simple;

/**
 * @ClassName: ReviewTest
 * @Description: 剑指offer复习测试
 * @Author: WM
 * @Date: 2021-10-18 14:53
 **/
public class ReviewTest {
    public static void main(String[] args) {
        System.out.println(divide(-2147483648, -1));
    }

    private static int divide(int a, int b) {
        long num1 = a;
        long num2 = b;
        long result = 0;
        boolean isLessZero = false;
        if ((num1 > 0 && num2 < 0) || (num1 < 0 && num2 > 0)) {
            isLessZero = true;
        }
        if (num1 < 0) num1 = -num1;
        if (num2 < 0) num2 = -num2;
        while (num1 >= num2) {
            long temp = num2;
            long count = 1;
            while (num1 >= temp) {
                temp <<= 1;
                count <<= 1;
            }
            temp >>= 1;
            count >>= 1;
            num1 -= temp;
            result += count;
        }
        if (isLessZero) {
            result = -result;
        }
        // 精度判断
        if (result > Integer.MAX_VALUE || result < Integer.MIN_VALUE) {
            result = Integer.MAX_VALUE;
        }
        return (int) result;
    }
}
